﻿/*
    2008.05.26 italki create, ajax异步请求处理。
 */

namespace Web.Control.WWW.WebIM
{

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    using Model.WWW_User.User;
    using Model.WWW_WWW.WebIM;
    using BLF.WWW_WWW.WebIM;

    /// <summary>
    /// ajax异步请求处理类
    /// </summary>
    public class asyproxy : Page
    {
        public asyproxy() {

        }//GetDetailUserInfo

        protected override void OnLoad(EventArgs e) {
            //用于输出的XML StringBuilder
            StringBuilder outputXMLBuilder = new StringBuilder();
            outputXMLBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            outputXMLBuilder.Append("<result>");

            //登录判断
            if (HttpContext.Current.User.Identity.Name == "") {
                outputXMLBuilder.Append(AppendErrorNode("User is log out!"));
                outputXMLBuilder.Append("<content />");
            } else {
                switch (Request.QueryString["action"]) {
                    #region 获得好友列表
                    case "getfriendlist":
                        List<UserSummaryInfoModel> friendList = BLF.WWW_User.Users.User.GetFriendList(HttpContext.Current.User.Identity.Name);
                        if (friendList != null) {
                            outputXMLBuilder.Append("<error />");
                            outputXMLBuilder.Append("<content>");
                            for (int i = 0; i < friendList.Count; i++) {
                                outputXMLBuilder.AppendFormat("<friend id=\"{0}\" avatar=\"{1}\" avatar_title=\"{2}\" motherlanguage=\"{3}\" targetlanguage=\"{4}\" nickname=\"{5}\" online=\"{6}\" />"
                                    , friendList[i].MemberNo.ToLower(), friendList[i].Avatar, friendList[i].Avatar_title, friendList[i].MotherTongue, friendList[i].TargetLanguage, friendList[i].Nickname, friendList[i].Online);
                            }
                            outputXMLBuilder.Append("</content>");
                        } else {
                            outputXMLBuilder.Append(AppendErrorNode("System error!"));
                            outputXMLBuilder.Append("<content />");
                        }
                        break;
                    #endregion

                    #region 获得谈过话的用户列表
                    case "gettalkedlist":
                        List<UserSummaryInfoModel> talkedList = BLF.WWW_User.Users.User.GetTalkedList(HttpContext.Current.User.Identity.Name);
                        if (talkedList != null) {
                            outputXMLBuilder.Append("<error />");
                            outputXMLBuilder.Append("<content>");
                            for (int i = 0; i < talkedList.Count; i++) {
                                outputXMLBuilder.AppendFormat("<" + (talkedList[i].IsFriend ? "friend" : "other") + " id=\"{0}\" avatar=\"{1}\" avatar_title=\"{2}\" motherlanguage=\"{3}\" targetlanguage=\"{4}\" nickname=\"{5}\" online=\"{6}\" />"
                                    , talkedList[i].MemberNo.ToLower(), talkedList[i].Avatar, talkedList[i].Avatar_title, talkedList[i].MotherTongue, talkedList[i].TargetLanguage, talkedList[i].Nickname, talkedList[i].Online);
                            }

                            outputXMLBuilder.Append("</content>");
                        } else {
                            outputXMLBuilder.Append(AppendErrorNode("System error!"));
                            outputXMLBuilder.Append("<content />");
                        }
                        break;
                    #endregion

                    #region 删除谈过话的用户记录
                    case "deltalked":
                        if (Request.QueryString["memberno"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                        } else {
                            int delcount = BLF.WWW_User.Users.User.DelTalked(HttpContext.Current.User.Identity.Name, Request.QueryString["memberno"]);
                            if (delcount <= 0) {
                                outputXMLBuilder.Append(AppendErrorNode("System Error"));
                                outputXMLBuilder.Append("<content />");
                            } else {
                                outputXMLBuilder.Append("<error />");
                                outputXMLBuilder.AppendFormat("<content>{0}</content>", delcount);
                            }
                        }

                        break;
                    #endregion

                    #region 获得用户基本信息
                    case "getuserinfo":
                        if (Request.QueryString["memberno"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                        } else {
                            //ArrayList _tmp = null;
                            UserSummaryInfoModel model = BLF.WWW_User.Users.User.GetUserBaseProfile(Request.QueryString["memberno"]);
                            if (model == null) {
                                outputXMLBuilder.Append(AppendErrorNode("No user!"));
                                outputXMLBuilder.Append("<content />");
                            } else {
                                outputXMLBuilder.Append("<error />");
                                outputXMLBuilder.Append("<content>");
                                outputXMLBuilder.AppendFormat("<other id=\"{0}\" avatar=\"{1}\" avatar_title=\"{2}\" motherlanguage=\"{3}\" targetlanguage=\"{4}\" nickname=\"{5}\" online=\"{6}\" />"
                                    , model.MemberNo.ToLower()
                                    , model.Avatar
                                    , model.Avatar_title
                                    , model.MotherTongue
                                    , model.TargetLanguage
                                    , model.Nickname
                                    , model.Online
                                    );
                                outputXMLBuilder.Append("</content>");
                            }
                        }

                        break;
                    #endregion

                    #region 请求加为好友
                    case "addfriend":
                        if (Request.QueryString["memberno"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                            break;
                        }
                        string useremail = BLF.WWW_User.Users.User.GetUserEmail(Request.QueryString["memberno"]);
                        if (useremail.Length > 0) {
                            string memberno = null;
                            outputXMLBuilder.Append("<error />");
                            outputXMLBuilder.Append("<content>");
                            switch (BLF.WWW_User.Users.User.AddIssue(HttpContext.Current.User.Identity.Name, useremail, out memberno)) {
                                case "-1":
                                    outputXMLBuilder.Append("No user!");
                                    break;
                                case "0":
                                    outputXMLBuilder.Append("He(She) is your friend!");
                                    break;
                                case "1":
                                case "2":
                                    outputXMLBuilder.Append("Request send Okay, please wait for your friend accept this request!");
                                    break;


                            }
                            outputXMLBuilder.Append("</content>");
                        }
                        break;
                    #endregion

                    #region 获得聊天历史
                    case "getchatlog":
                        if (Request.QueryString["jid"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                            break;
                        }

                        List<ChatLog> chatlogs = BLF.WWW_WWW.WebIM.asyproxy.GetChatLog(HttpContext.Current.User.Identity.Name, Request.QueryString["jid"]);
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.Append("<content>");
                        for (int i = 0; i < chatlogs.Count; i++) {
                            outputXMLBuilder.AppendFormat("<chatlog conversationid=\"{0}\" fromjid=\"{1}\" tojid=\"{2}\" sentdate=\"{3}\" id=\"{4}\"><![CDATA[{5}]]></chatlog>"
                                , chatlogs[i].ConversationID
                                , chatlogs[i].FromJID
                                , chatlogs[i].ToJID
                                , chatlogs[i].SentDate
                                , chatlogs[i].ID
                                , chatlogs[i].Body
                                );
                        }
                        outputXMLBuilder.Append("</content>");
                        break;
                    #endregion

                    #region 删除好友
                    case "removefriend":
                        if (Request.QueryString["friendid"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                            break;
                        }
                        BLF.WWW_WWW.Contact.Contacts.DeleContacts(HttpContext.Current.User.Identity.Name, Request.QueryString["friendid"]);
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.AppendFormat("<content>{0}</content>", "operate succeed!");
                        break;
                    #endregion

                    #region 设置自定义状态
                    case "setstate":
                        if (Request.QueryString["state"] == null) {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                            break;
                        }
                        int result = BLF.WWW_User.Users.User.SetUserState(HttpContext.Current.User.Identity.Name, Request.QueryString["state"]);
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.AppendFormat("<content>{0}</content>", result);
                        break;
                    #endregion

                    #region 获得自定义状态
                    case "getstate":
                        string state = BLF.WWW_User.Users.User.GetUserState(HttpContext.Current.User.Identity.Name).Trim();
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.AppendFormat("<content>{0}</content>", state);
                        break;
                    #endregion

                    #region 获得离线消息数目
                    case "getofflinemsgcount":
                        int count = BLF.WWW_WWW.WebIM.asyproxy.GetOfflineMessageCount(HttpContext.Current.User.Identity.Name);
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.AppendFormat("<content>{0}</content>", count);
                        break;
                    #endregion

                    #region 获得用户在离线状态
                    case "getuseronline":
                        string[] userJids = null;
                        try {
                            userJids = Request.QueryString["userjids"].Split(',');
                            if (userJids.Length <= 0) throw new Exception();
                        } catch {
                            outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                            outputXMLBuilder.Append("<content />");
                            break;
                        }
                        string[] userStates = BLF.WWW_WWW.WebIM.asyproxy.GetUserOnline(userJids);
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.Append("<content>");
                        for (int i = 0; i < userStates.Length; i++) {
                            string[] tmp = userStates[i].Split(',');
                            outputXMLBuilder.AppendFormat("<userstate id=\"{0}\" online=\"{1}\"></userstate>", tmp[0], tmp[1]);
                        }
                        outputXMLBuilder.Append("</content>");
                        break;
                    #endregion

                    #region 清除Session
                    case "clearsession":
                        Session["onlyAwindow"] = null;
                        outputXMLBuilder.Append("<error />");
                        outputXMLBuilder.Append("<content />");
                        break;
                    #endregion

                    #region 不正确的参数
                    default:
                        outputXMLBuilder.Append(AppendErrorNode("Params error!"));
                        outputXMLBuilder.Append("<content />");
                        break;
                    #endregion
                }
            }

            outputXMLBuilder.Append("</result>");

            Response.ContentType = "text/xml";
            Response.Write(outputXMLBuilder.ToString());

            /*
            StringBuilder outputHTML = new StringBuilder();
            switch (Request.QueryString["action"])
            {
                #region 获得好友列表
                case "getfriendlist":
                    ArrayList profiles;
                    BLF.WWW_User.Users.User.GetUserProfile(HttpContext.Current.User.Identity.Name, 1, 99999, out profiles);
                    if (profiles != null && profiles.Count > 0)
                    {
                        List<MemberSimpleModel> friendList = profiles[0] as List<MemberSimpleModel>;
                        for (int i = 0; i < friendList.Count; i++)
                        {
                            outputHTML.Append("<div style=\"color:red;\">system error</div>");
                        }
                    }
                    else {
                        outputHTML.Append("<div style=\"color:red;\">system error</div>");
                    }
                    break;
                #endregion
            }
            */

            base.OnLoad(e);
        }

        private string AppendErrorNode(string errorText) {
            return string.Format("<error>{0}</error>", errorText);
        }
    }

}